/**
 * Copyright 2012 ibiblio
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ibiblio.terasaur.bt

import org.ibiblio.terasaur.bencode.ByteArray

/**
 * Represents an element of a torrent.info.files array.  Not intended to be
 * used as an independent class.  See TorrentMetainfo.
 *
 * Note that some BT clients insert non-standard file metadata elements. We
 * save these for subsequent regeneration of torrent files, but they're not
 * exposed through the TorrentMetainfo API.
 */
class TorrentMetainfoFileinfo {
    public String path // file path relative to the torrent's name (root path)
    public Long length // file length in bytes
    public String md5sum // optional
    private Map _extras // non-standard file information

    /**
     * Input Map should be a single element of a torrent.info.files array.
     *
     * @param fileinfo
     */
    public TorrentMetainfoFileinfo(Map fileinfo) {
        this.length = fileinfo.get('length')
        this.path = fileinfo.get('path').join(File.separator)
        if (fileinfo.containsKey('md5sum')) {
            this.md5sum = fileinfo.get('md5sum')
        }
        _saveExtraElements(fileinfo)
    }

    private _saveExtraElements(Map fileinfo) {
        def keys = fileinfo.keySet() as List
        keys.removeAll(['length', 'path', 'md5sum'])
        if (keys.size() > 0) {
            _extras = [:]
            keys.each {key->
                _extras[key] = fileinfo.get(key)
            }
        }
    }

    /**
     * Return file info data as a nicely-formatted string.
     *
     * @return String
     */
    public String toString() {
        def s = "        TorrentMetainfoFileinfo [\n"
        s += '            path: ' + this.path + "\n"
        s += '            length: ' + this.length + "\n"
        s += '            md5sum: ' + this.md5sum + "\n"
        s += "        ]\n"
        return s
    }

    /**
     * Return file info data in a Map
     *
     * @return Map
     */
    public Map toMap() {
        Map data = [
            'length': this.length,
            'path': []
            ]
        this.path.split('/').each { data.path << new ByteArray(it.bytes) }
        if (this.md5sum) {
            data.md5sum = new ByteArray(this.md5sum.bytes)
        }

        if (_extras) {
            _extras.each {key, value->
                if (value instanceof String) {
                    data[key] = new ByteArray(value.bytes)
                } else {
                    data[key] = value
                }
            }
        }

        return data
    }

    public boolean equals(TorrentMetainfoFileinfo b) {
        if (path.equals(b.path) && (length == b.length) && md5sum.equals(b.md5sum) && _extras.equals(b._extras)) {
            return true
        } else {
            return false
        }
    }
}
